package com.dong.zk;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author: dzj
 * @date 2018/10/10-13:30
 */
public class ZkGetChildrenNode extends BaseWatcher {
    public static CountDownLatch cdl = new CountDownLatch(1);


    public static Logger logger = LoggerFactory.getLogger(ZkGetNodeData.class);

    public ZkGetChildrenNode() {
        super(logger,zkServerPath);
    }

    public static void main(String[] args) throws KeeperException, InterruptedException {
        ZkGetChildrenNode zkGetChildrenNode = new ZkGetChildrenNode();
       /* List<String> children = zkGetChildrenNode.getZooKeeper().getChildren("/imooc", true);
        System.out.println(children);*/

        String ctx = "{callback:childrenCallBack}";
      //  zkGetChildrenNode.getZooKeeper().getChildren("/imooc",true,new ChildrenCallBack1(),ctx);
        zkGetChildrenNode.getZooKeeper().getChildren("/imooc",true,new ChildrenCallback2(),ctx);
        cdl.await();
    }

    public void process(WatchedEvent watchedEvent) {
        try{
            if(watchedEvent.getType()== Event.EventType.NodeDataChanged) {


            }else if (watchedEvent.getType()== Event.EventType.NodeCreated) {

            }else if(watchedEvent.getType()==Event.EventType.NodeDeleted) {

            }else if (watchedEvent.getType()==Event.EventType.NodeChildrenChanged) {
                ZkGetNodeData zkServer = new ZkGetNodeData(zkServerPath);
              List<String> children = zkServer.getZk().getChildren("/imooc", false);

                System.out.println(children);





                cdl.countDown();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
